package org.karelun.gef.trash;

import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.Viewport;
import org.eclipse.draw2d.parts.ScrollableThumbnail;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.editparts.ScalableRootEditPart;
import org.eclipse.gef.ui.parts.ContentOutlinePage;
import org.eclipse.gef.ui.parts.SelectionSynchronizer;
import org.eclipse.gef.ui.parts.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.karelun.gef.model.DiagramModel;
import org.karelun.gef.outline.TreePartFactory;

public class OutlinePage extends ContentOutlinePage {
	
	private SashForm sash;

	private ScrollableThumbnail thumbnail;

	private DisposeListener disposeListener;

	private AbstractDiagramEditor content;

	private SelectionSynchronizer selectionSyn;

	private GraphicalViewer viewer;

	public OutlinePage(AbstractDiagramEditor content) {
		super(new TreeViewer());
		this.content = content;
		 selectionSyn=content.getSelectionSynchronizer();
		 viewer=content.getGraphicalViewer();
	}
	
//	public void init(IPageSite pageSite) {
//		super.init(pageSite);
//		ActionRegistry registry = content.getActionRegistry();
//		IActionBars bars = pageSite.getActionBars();
//		String id = IWorkbenchActionConstants.UNDO;
//		bars.setGlobalActionHandler(id, registry.getAction(id));
//		id = IWorkbenchActionConstants.REDO;
//        bars.setGlobalActionHandler(id, registry.getAction(id));
//        id = IWorkbenchActionConstants.DELETE;
//        bars.setGlobalActionHandler(id, registry.getAction(id));
//        bars.updateActionBars();
//	}
	
	public void createControl(Composite parent) {
		sash = new SashForm(parent, SWT.VERTICAL);
		getViewer().createControl(sash);
        getViewer().setEditDomain(content.getEditDomain());
		getViewer().setEditPartFactory(new TreePartFactory());
		DiagramModel model = content.getDiagram();
		getViewer().setContents(model);

		selectionSyn.addViewer(getViewer());

		Canvas canvas = new Canvas(sash, SWT.BORDER);
		LightweightSystem lws = new LightweightSystem(canvas);

		thumbnail = new ScrollableThumbnail(
				(Viewport) ((ScalableRootEditPart) viewer.getRootEditPart())
						.getFigure());
		thumbnail.setSource(((ScalableRootEditPart) viewer.getRootEditPart())
				.getLayer(LayerConstants.PRINTABLE_LAYERS));

		lws.setContents(thumbnail);

		disposeListener = new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				if (thumbnail != null) {
					thumbnail.deactivate();
					thumbnail = null;
				}
			}
		};
		viewer.getControl().addDisposeListener(disposeListener);
	}

	public Control getControl() {
		return sash;
	}

	public void dispose() {
		 selectionSyn.removeViewer(getViewer());
		 Control control = viewer.getControl();
		 if (control != null && !control.isDisposed()) {
		 control.removeDisposeListener(disposeListener);
		 }
		 super.dispose();
	}
}
